<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Aktifitas extends zulController{
	
	function __construct(){
		parent::__construct();
		$this->load->model("aktifitas_mahasiswa_model");
		$this->load->model("mahasiswa_model");
		$this->checkLogin();
	}
	public function index()
	{
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
        $colModel['NAMA'] = array('Nama Mahasiswa', 200, FALSE, 'center', 1);
        $colModel['KETERANGAN'] = array('Keterangan', 200, FALSE, 'center', 1);
		$colModel['PATH'] = array('Bukti Sertifikat', 110, FALSE, 'center', 1);
		$colModel['JENIS'] = array('Jenis', 150, FALSE, 'center', 1);
		$colModel['EDIT'] = array('Edit', 70, FALSE, 'center', 0);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Aktifitas Mahasiswa'
        );
        $buttons[] = array('Tambah', 'add', 'aktifitas');
        $buttons[] = array('Hapus', 'delete', 'aktifitas');
        $buttons[] = array('separator');
        $buttons[] = array('Pilih Semua', 'add', 'aktifitas');
        $buttons[] = array('Hapus Pilihan', 'delete', 'aktifitas');
        $buttons[] = array('separator');
		//$buttons[] = array('Rekap Stok Obat', 'print', 'aktifitas');
		
        $grid_js = build_grid_js('flex1', site_url("aktifitas/grid_aktifitas"), $colModel, 'ID_AKTIFITAS', 'ASC', $gridParams, $buttons);

        $data['title'] = "Daftar Aktifitas Mahasiswa";
        $data['info'] = "Daftar Aktifitas Mahasiswa";
        $data['js_grid'] = $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function aktifitas(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				if (com=='Tambah')
				{
					location.href='" . base_url() . "index.php/aktifitas/form_aktifitas/'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .base_url('index.php/aktifitas/delete_aktifitas')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["active"] = "master";
		$data["content"]	= $this->load->view("grid",$data,true);
		$this->load->view("main_new",$data);
	}
	function grid_aktifitas(){
		$this->load->helper("file");
		$valid_fields = array('ID_AKTIFITAS', 'NAMA', 'KETERANGAN', 'PATH', 'JENIS');
        $this->flexigrid->validate_post('ID_AKTIFITAS', 'ASC', $valid_fields);
		$join	= array(
			array("table"=>"aktifitas_mahasiswa","join_key"=>"ID_MAHASISWA","join_table"=>"master_mahasiswa","join_type"=>"left"),
		);
		$column	= array('ID_AKTIFITAS', 'NAMA', 'KETERANGAN', 'PATH', 'JENIS');
		if($this->session->userdata("id_role") == 1){
        $records = $this->aktifitas_mahasiswa_model->get_joinFG($join,FALSE,$column);
        } else {
		
		$records = $this->aktifitas_mahasiswa_model->get_joinFG($join,array("aktifitas_mahasiswa.ID_MAHASISWA"=>$this->session->userdata("id_mahasiswa")),$column);
		}
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
		
        foreach ($records['records']->result() as $row) {
			$data_upload	= base_url()."uploads/".$row->PATH;
			$data_upload	= get_mime_by_extension($data_upload);
			$data_upload	= explode("/",$data_upload);
			if($data_upload[0]	== "image"){
				$data_upload	= "<a class='fancybox' href='".base_url()."uploads/".$row->PATH."'><img src='".base_url()."assets/images/grid/magnifier.png'></a>";
			} else {
				$data_upload	= "<a href=\"javascript:alert('Bukti sertifikat tidak tersedia !')\"\"><img src='".base_url()."assets/images/grid/magnifier.png'></a>";
			}
            $record_items[] = array($row->ID_AKTIFITAS,
                $no,
				$row->NAMA,
				$row->KETERANGAN,
				$data_upload,
				($row->JENIS == 1) ? ("Prestasi") : ("Aktifitas Kemahasiswaan"),
				"<a href='".site_url("/aktifitas/form_aktifitas/".$row->ID_AKTIFITAS)."'><img src='". base_url()."assets/images/grid/edit.png'></a>"
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	function form_aktifitas($parameter=FALSE){
		$this->load->library("form_validation");
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in">', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'nama','label'=>'Nama Kegiatan','rules'=>'required'),
		array('field'=>'ket','label'=>'Keterangan','rules'=>'required'),
		array('field'=>'jenis','label'=>'Jenis','rules'=>'required'),
		));
		// ($this->input->post("jenis") == 1) ? ($this->form_validation->set_rules("userfile","Bukti Sertifikat","required")) : ("");
		($this->session->userdata("id_role") == 1) ? ($this->form_validation->set_rules("mahasiswa","Nama Mahasiswa","required")) : ("");
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		if($this->form_validation->run() == FALSE){
			if($parameter == TRUE){
				$data["value"] = $this->aktifitas_mahasiswa_model->get($parameter);
			}
			$data["parameter"]	= $parameter;
			$data["q_mahasiswa"]= $this->mahasiswa_model->get();
			$data["active"] = "master";
			$data["content"]	= $this->load->view("form_aktifitas",$data,true);
			$this->load->view("main_new",$data);
		} else {
			
			$nama	= $this->input->post("nama");
			($this->session->userdata("id_role") == 1) ? ($mahasiswa = $this->input->post("mahasiswa")) : ($mahasiswa = $this->session->userdata("id_mahasiswa"));
			$ket	= $this->input->post("ket");
			$jenis	= $this->input->post("jenis");
			$config['upload_path'] = './uploads/';
			$config['encrypt_name']	= FALSE;
			$config['allowed_types'] = 'jpg|png|gif';
			$config['file_name'] = $nama . '_' . ($this->session->userdata("id_role") == 2) ? ($this->session->userdata("id_mahasiswa")) : ($mahasiswa);
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload()){
				echo "oke";
			}
			if($_FILES['userfile']['name']){
				$file_name	= $this->upload->data();
			}else{
				$file_name	= "";
			}
			if($_FILES['userfile']['name']){
				$data	= array(
				"ID_MAHASISWA"=>$mahasiswa,
				"NAMA_KEGIATAN"=>$nama,
				"KETERANGAN"=>$ket,
				"PATH"=>$file_name['file_name'],
				"JENIS"=>$jenis,
				);
			} else {
				$data	= array(
				"ID_MAHASISWA"=>$mahasiswa,
				"NAMA_KEGIATAN"=>$nama,
				"KETERANGAN"=>$ket,
				"JENIS"=>$jenis,
				);
			}
			$this->aktifitas_mahasiswa_model->save($data, $parameter);
			redirect("/aktifitas");
		}
	}
	function delete_aktifitas(){
		$items=$this->input->post('items');
		if($items){
			$id_array = explode(",",$items);
			$id=$id_array[0];
			$this->aktifitas_mahasiswa_model->delete($id);
		}
		foreach($id_array as $index => $id){
			if (isset($id) && is_numeric($id)){
				$this->aktifitas_mahasiswa_model->delete($id);
				$msg ='Data yang anda pilih berhasil dihapus';
			}
		}
		$this->output->set_output($msg);
	}
}
?>